//
//  STSignInView.swift
//  iPhone
//
//  Created by Toj on 4/19/23.
//

import UIKit

protocol STSignInViewDelegate: NSObjectProtocol {
    func signInViewDidTapOthers(_ signInView: STSignInView)
    func signInViewDidTapSignin(_ signInView: STSignInView)
    func signInViewDidTapClose(_ signInView: STSignInView)
}

class STSignInView: UIView {
    
    weak var delegate: STSignInViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    /* 背景View */
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.tt_alpha(0.5)
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(didTapBackground))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    /* 展示内容 */
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.cm_corner(radius: 24, corners: [.layerMinXMinYCorner, .layerMaxXMinYCorner])
        return view
    }()
    
    /* 关闭按钮 */
    private lazy var closeButton: UIButton = {
        let view = UIButton()
        view.setImage("com_close_lucencyGray".img_resource, for: .normal)
        view.addTarget(self, action: #selector(didTapClosed), for: .touchUpInside)
        return view
    }()
    
    /* 标题 */
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#333333".to_color
        view.font = 20.to_boldFont
        view.textAlignment = .center
        view.numberOfLines = 0
        view.text = "NewSignIn_title1".localized()
        return view
    }()
    
    /* 上次缓存的账号 */
    private lazy var accountLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#0087EB".to_color
        view.font = 16.to_boldFont
        view.textAlignment = .center
        return view
    }()
    
    /* 登录按钮 */
    private lazy var signInButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#0087EB".to_color
        view.titleLabel?.font = 14.to_boldFont
        view.setTitleColor(.white, for: .normal)
        view.setTitle("NewSignIn_title1".localized(), for: .normal)
        let width = CGFloat.sr_width - 80
        view.frame = CGRect(origin: .zero, size: CGSize(width: width, height: 32.ui_scale))
        view.addTarget(self, action: #selector(didTapSignin), for: .touchUpInside)
        view.cm_cornerRadius = 16.ui_scale
        return view
    }()
    
    /* 其他登录方式 */
    private lazy var otherAccountButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = contentView.backgroundColor
        view.setTitleColor("#0087EB".to_color, for: .normal)
        let attributedTitle = "NewSignIn_OtherAccount".localized().to_attributedString(font: 14.to_boldFont, lineStyle: .underline)
        view.setAttributedTitle(attributedTitle, for: .normal)
        view.addTarget(self, action: #selector(didTapOtherAccount), for: .touchUpInside)
        return view
    }()
    
    private lazy var privacyView: STSignInPrivacyView = {
        let view = STSignInPrivacyView()
        return view
    }()
}

// MARK: - 点击事件
@objc private extension STSignInView {
    
    func didTapBackground() {
        didTapClosed()
    }
    
    func didTapClosed() {
        print("-->toj: func:" + #function)
        delegate?.signInViewDidTapClose(self)
    }
    
    func didTapSignin() {
        print("-->toj: func:" + #function)
        delegate?.signInViewDidTapSignin(self)
    }
    
    func didTapOtherAccount() {
        print("-->toj: func:" + #function)
        delegate?.signInViewDidTapOthers(self)
    }
}

// MARK: - Public
extension STSignInView {
    
    func show() {
        isHidden = false
        setupMakeAccount()
    }
    
    func hide() {
        isHidden = true
    }
}

// MARK: - Private
private extension STSignInView {
    
}

// MARK: - 初始化设置
private extension STSignInView {
    
    func setupMakeAddSubviews() {
        
        addSubview(backgroundView)
        
        addSubview(contentView)
        
        // 标题
        contentView.addSubview(titleLabel)
        // 关闭按钮
        contentView.addSubview(closeButton)
        // 账号快捷键
        contentView.addSubview(accountLabel)
        // 登录按钮
        contentView.addSubview(signInButton)
        // 其他登录方式
        contentView.addSubview(otherAccountButton)
        // 服务协议
        contentView.addSubview(privacyView)
    }
    
    func setupMakeLayoutSubviews() {
        
        backgroundView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        contentView.snp.makeConstraints { make in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(330.ui_mscale)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.top.equalTo(contentView)
            make.width.height.equalTo(48)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalTo(contentView).inset(20)
            make.top.equalTo(contentView)
            make.height.equalTo(100.ui_mscale)
        }
        
        accountLabel.snp.makeConstraints { make in
            make.centerX.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(8.ui_mscale)
            make.height.equalTo(20.ui_mscale)
        }
        
        let size = signInButton.frame.size
        signInButton.snp.remakeConstraints { make in
            make.centerX.equalTo(contentView)
            make.top.equalTo(accountLabel.snp.bottom).offset(24.ui_mscale)
            make.width.equalTo(size.width)
            make.height.equalTo(size.height)
        }
        
        otherAccountButton.snp.makeConstraints { make in
            make.height.left.right.equalTo(signInButton)
            make.top.equalTo(signInButton.snp.bottom).offset(10.ui_mscale)
        }
        
        privacyView.snp.makeConstraints { make in
            make.left.right.equalTo(signInButton)
            make.height.equalTo(100.ui_mscale)
            make.bottom.equalTo(self)
        }
    }
    
    func setupMakeInitialize() {
        
    }
    
    func setupMakeAccount() {
        let accountRandom = arc4random_uniform(3)
        switch accountRandom {
        case 0:
            let phone = "23456789"
            accountLabel.text = phone.st_confusePhone(replace: "*****")
        case 1:
            let email = "E1234567@123.com"
            accountLabel.text = email.st_confusePhone(replace: "**")
        default:
            accountLabel.text = setupMakeOtherAccount()
        }
    }
    
    func setupMakeOtherAccount() -> String {
        return "Other"
    }
}
